﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MatrixFunTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program main = new Program();

            double[,] start = main.createMatrix();
            main.Write(start);
            Console.WriteLine("-------------------------------------");
            Console.WriteLine(main.Equal(main.RotateMatrixRight(main.RotateMatrixRight(main.TransposeMatrix(start))), main.TransposeMatrix(main.RotateMatrixRight(main.RotateMatrixRight(start)))));
            Console.WriteLine("TestAB");
            Console.WriteLine(main.Equal(start, main.TestA(main.TestB(start))));
            main.Write(main.TestA(main.TestB(start)));
            Console.WriteLine("-------------------------------------");
            Console.WriteLine(main.Equal(main.RotateMatrixRight(start), main.UtiliseSymmetrie(start,0,0)));
            Console.WriteLine(main.Equal(main.RotateMatrixRight(main.TransposeMatrix(start)), main.UtiliseSymmetrie(start, 1, 0)));
            Console.WriteLine(main.Equal(start, main.UtiliseSymmetrie(start, 4, 0)));
            Console.WriteLine(main.Equal(start, main.UtiliseSymmetrie(start, 7, 0)));
            Console.WriteLine(main.Equal(start, main.UtiliseSymmetrie(start, 6, 1)));
            Console.WriteLine(main.Equal(main.RotateMatrixRight(main.TransposeMatrix(main.RotateMatrixLeft(start))), main.UtiliseSymmetrie(start, 6, 2)));
            Console.WriteLine(main.Equal(main.RotateMatrixRight(main.RotateMatrixRight(main.RotateMatrixRight(start))), main.UtiliseSymmetrie(start, 5, 4)));
            Console.WriteLine(main.Equal(main.RotateMatrixRight(main.RotateMatrixRight(main.RotateMatrixRight(start))), main.UtiliseSymmetrie(start, 5, 5)));
            Console.WriteLine(main.Equal(main.RotateMatrixLeft(main.TransposeMatrix(start)), main.UtiliseSymmetrie(start, 5, 6)));
            Console.ReadLine();
        }

        private bool Equal(double[,] a, double[,] b)
        {
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                    if (a[i, j] != b[i, j])
                        return false;
            return true;
        }

        private double[,] createMatrix()
        {
            double[,] result = new double[8, 8];
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                    result[i, j] = i * j - i * i + j * j;
            return result;
        }

        private void Write(double[,] writeMe)
        {
            for (int i = 0; i < 8; i++)
            {
                string line = "";
                for (int j = 0; j < 8; j++)
                    line += writeMe[i, j] + ",";
                Console.WriteLine(line);
            }
        }

        private void WriteSingleLine(double[] writeMe)
        {
            string line = "";
            for (int i = 0; i < 64; i++)
            {
                    line += writeMe[i] + ",";
                
            }

            Console.WriteLine(line);
        }

        private double[,] TestA(double[] tokenDoubleArray)
        {
            double[,] token2Dim = new double[8, 8];
            for (int i = 0; i < 64; i++)
            {
                token2Dim[i/8, i % 8] = tokenDoubleArray[i];
            }

            return token2Dim;
        }

        private double[] TestB(double[,] token2Dim)
        {
            double[] tokenDoubleArray = new double[64];
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    tokenDoubleArray[i * 8 + j] = token2Dim[i, j % 8];
                }
            }

            return tokenDoubleArray;
        }

        private double[,] UtiliseSymmetrie(double[,] token2Dim, int column, int row)
        {


            if (column < 4 && row < 4)
            {
                // 1. Quadrant
                if (column <= row)
                    token2Dim = RotateMatrixRight(token2Dim);
                else
                    token2Dim = RotateMatrixRight(TransposeMatrix(token2Dim));
            }

            if (column > 3 && row < 4)
            {
                // 2. Quadrant
                if ((row + column) <= 7)
                {
                    // korrekt
                }
                else
                    token2Dim = RotateMatrixRight(TransposeMatrix(RotateMatrixLeft(token2Dim)));
            }

            if (column > 3 && row > 3)
            {
                Console.WriteLine("3. Quadrant");
                // 3. Quadrant
                if (column >= row)
                {
                    token2Dim = RotateMatrixLeft(token2Dim);
                }
                else
                {
                    token2Dim = RotateMatrixLeft(TransposeMatrix(token2Dim));
                }
            }

            if (column < 4 && row > 3)
            {
                Console.WriteLine("4. Quadrant");
                // 4. Quadrant
                if ((row + column) <= 6)
                    token2Dim = RotateMatrixRight(TransposeMatrix(RotateMatrixRight(token2Dim)));
                else
                    token2Dim = RotateMatrixRight(RotateMatrixRight(token2Dim));
            }



            return token2Dim;
        }

        private double[,] TransposeMatrix(double[,] matrix)
        {
            int n = 8;
            double[,] ret = new double[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    ret[i, j] = matrix[j, i];
                }
            }

            return ret;
        }

        public double[,] RotateMatrixRight(double[,] matrix)
        {
            /* W and H are already swapped */
            int w = 8;
            int h = 8;
            double[,] ret = new double[h, w];
            for (int i = 0; i < h; ++i)
            {
                for (int j = 0; j < w; ++j)
                {
                    ret[i, j] = matrix[w - j - 1, i];
                }
            }
            return ret;
        }


        public double[,] RotateMatrixLeft(double[,] matrix)
        {
            /* W and H are already swapped */
            int w = 8;
            int h = 8;
            double[,] ret = new double[h, w];
            for (int i = 0; i < h; ++i)
            {
                for (int j = 0; j < w; ++j)
                {
                    ret[i, j] = matrix[j, h - i - 1];
                }
            }
            return ret;
        }
    }
}
